<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" placeholder="邮箱" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.mobile" placeholder="手机号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="merchant_id" label="商户id" align="center" />
          <el-table-column
            prop="cardholder_id"
            label="持卡人ID"
            align="center"
          />
          <el-table-column prop="email" label="邮箱" align="center" />
          <el-table-column prop="mobile_number" label="手机号" align="center" />
          <el-table-column prop="first_name" label="FirstName" align="center" />
          <el-table-column prop="laster_name" label="LastName" align="center" />
          <el-table-column
            prop="middle_name"
            label="MiddleName"
            align="center"
          />
          <el-table-column prop="title" label="头衔" align="center" />
          <el-table-column prop="date_of_birth" label="生日" align="center" />
          <el-table-column prop="gender" label="性别" align="center" />
          <el-table-column prop="nationality" label="国籍" align="center" />
          <el-table-column prop="id_number" label="证件号码" align="center" />
          <el-table-column
            prop="id_expiry_date"
            label="证件到期时间"
            align="center"
          />
          <el-table-column prop="id_type" label="证件类型" align="center" />
          <el-table-column prop="id_country" label="国家" align="center" />
          <el-table-column prop="id_state" label="地区" align="center" />
          <el-table-column label="个人地址" align="center">
            <template #default="scope">
              {{ scope.row.addr_country }}/{{ scope.row.addr_city }}/{{
                scope.row.addr_line1
              }}/{{ scope.row.addr_line2 }}/{{ scope.row.addr_postcode }}/{{
                scope.row.addr_state
              }}
            </template>
          </el-table-column>
          <el-table-column label="邮寄地址" align="center">
            <template #default="scope">
              {{ scope.row.postal_country }}/{{ scope.row.postal_city }}/{{
                scope.row.postal_line1
              }}/{{ scope.row.postal_line2 }}/{{ scope.row.postal_postcode }}/{{
                scope.row.postal_state
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, formatter } from "@/utils/utils";
import { cardsHoldersApi } from "@/api/ExAssistant/cards";
const loading = ref(false);
const tableData = ref([]);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  email: "",
  mobile: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    email: form.email,
    mobile: form.mobile,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await cardsHoldersApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
